/*
* File: CFdEventMonitor.h
* Copyright (c) 2010. Silviu Caragea <silviu@intelliproject.net>
* Created on September 14, 2010, 10:39 PM
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*  1.Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
*  2.Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
*  3.All advertising materials mentioning features or use of this software
*    must display the following acknowledgement:
*       This product includes software developed by the University of
*       California, Berkeley and its contributors.
*  4.Neither the name of the University nor the names of its contributors
*    may be used to endorse or promote products derived from this software
*    without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#ifndef CFDEVENTMONITOR_H
#define	CFDEVENTMONITOR_H

#include "CFdEventFactory.h"

#ifdef HAVE_MULTITHREAD_LITEVENT
    #include "../utils/CCriticalSection.h"
#endif

#define EVENT_LOOP_ONCE SHIFT_LEFT(1,0)
#define EVENT_LOOP_NONBLOCK SHIFT_LEFT(1,1)

namespace litevent
{

#ifdef USE_SELECT
    extern const struct EventMothodSignature sigSelect;
#endif

#ifdef USE_EPOLL
    extern const struct EventMothodSignature sigEpoll;
#endif

#ifdef USE_POLL
    extern const struct EventMothodSignature sigPoll;
#endif

#ifdef USE_KQUEUE
    extern const struct EventMothodSignature sigKqueue;
#endif

#ifdef USE_SOLARIS_DEVPOLL
    extern const struct EventMothodSignature sigDevPoll;
#endif


class CFdEventMonitor
{
public:
    CFdEventMonitor(EnumEventHandlerType eventPollType , size_t maxfds = 4096);
    ~CFdEventMonitor();
 
    void Loop(int attributes = 0);
    int QuickPoll(int timeOut);

    inline bool isInitialised();
    inline void ResetWatcher();

    template <class K> inline int SetEventWatcher(int fd, void*context, int events, K *obj,void(K::*method)(int, void*,int));
       
    inline int SetEventWatcher(int fd, fd_event_handler handler, void*context, int events);
    inline void RemoveEventWatcher(int fd);
    inline void ChangeFdEvent(int fd, int events);

    static const char** getAvailableMethods();
    const char * getCurrentMethod();

private:
    const char *methodName;
    EnumEventHandlerType pollType;
    IFdEventInterface *evInterface;

    DISALLOW_IMPLICIT_CONSTRUCTORS(CFdEventMonitor);

#ifdef HAVE_MULTITHREAD_LITEVENT
    util::CCriticalSection m_lock;
#endif

};

}

#endif	/* CFDEVENTMONITOR_H */

